package com.lamda.worker.manager;

import com.lamda.worker.command.CommandExecutor;
import com.lamda.worker.model.Vps;
import com.lamda.worker.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MJiao on 15/8/27.
 */
@Component
public class CommandManager {

    @Autowired
    private CommandExecutor commandExecutor;


    /**
     * 获取该主机上的所有vps
     *
     * @return
     */
    public List<Vps> getAllVps() {
        List<Vps> vpsList = new ArrayList<>();
        try {
            vpsList.addAll(this.commandExecutor.executeGetAllVps());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return vpsList;
    }

    public ResultVo restart(String computerName, String vmName) {
        try {
            return this.commandExecutor.executeRestart(computerName, vmName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

    public ResultVo start(String computerName, String vmName) {
        try {
            return this.commandExecutor.executeStart(computerName, vmName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResultVo();
    }

    public ResultVo stop(String computerName, String vmName) {
        try {
            return this.commandExecutor.executeStop(computerName, vmName, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResultVo();
    }
}
